package com.newsplatform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newsplatform.entity.News;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 新闻Mapper接口
 * 
 * @author Logan.Li
 */
@Mapper
public interface NewsMapper extends BaseMapper<News> {
    
    /**
     * 分页查询新闻（带分类名称）
     */
    @Select("SELECT n.*, c.name as category_name, u.nickname as author_name " +
            "FROM news n " +
            "LEFT JOIN category c ON n.category_id = c.id " +
            "LEFT JOIN user u ON n.author_id = u.id " +
            "WHERE n.status = #{status} " +
            "ORDER BY n.is_top DESC, n.publish_time DESC")
    IPage<News> selectNewsWithCategory(Page<News> page, @Param("status") Integer status);
    
    /**
     * 增加浏览次数
     */
    @Update("UPDATE news SET view_count = view_count + 1 WHERE id = #{id}")
    void incrementViewCount(@Param("id") Long id);
    
    /**
     * 增加点赞次数
     */
    @Update("UPDATE news SET like_count = like_count + 1 WHERE id = #{id}")
    void incrementLikeCount(@Param("id") Long id);
    
}
